import { createSlice } from "@reduxjs/toolkit";

export const AddTickPreson = createSlice({
  name: "counter",
  initialState: {
    tickpreson: [
      {
        id: '1',
        name: "张三",
        code: "130452586595666598",
        phone: "15188041798",
        type: "身份证",
        status: false,
      },
      {
        id: '2',
        name: "李四",
        code: "130452586595666598",
        phone: "15188041798",
        type: "身份证",
        status: true,
      },
      {
        id: '3',
        name: "王五",
        code: "130452586595666598",
        phone: "15188041798",
        type: "身份证",
        status: false,
      },
    ],
  },
  reducers: {
    selectTick(state, { payload }) {
      const item = state.tickpreson.find((item) => item.id === payload.id);
      if (item) {
        item.status = !item.status;
      }
    },
    editTick(state, { payload }) {
      const item = state.tickpreson.find((item) => item.id === payload.id);
      if (item) {
        Object.assign(item);
      }
    },
    addTick(state, { payload }) {
      const item = state.tickpreson.find((item) => item.id !== payload.id);
      if (item) {
        state.tickpreson.push(item);
      }
    },
    delTick(state,{payload}){
      state.tickpreson = state.tickpreson.filter(
        (item)=>item.id !== payload.id 
      )
    }
  },
});

// Action creators are generated for each case reducer function
export const { selectTick, editTick, addTick, delTick } = AddTickPreson.actions;

export default AddTickPreson.reducer;
